package com.trivia.fortune.sdk.ad;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;

import com.trivia.fortune.sdk.TrFConstant;

import sg.bigo.ads.api.AdLoadListener;
import sg.bigo.ads.api.RewardAdInteractionListener;
import sg.bigo.ads.api.RewardVideoAd;
import sg.bigo.ads.api.RewardVideoAdLoader;
import sg.bigo.ads.api.RewardVideoAdRequest;

public class TrFBigoRewardVideo implements TrFVideoIAP {

    private static TrFBigoRewardVideo adManager;

    private TrFVideoRewardCallback mListner;

    private boolean loadFinish=true;

    public static TrFBigoRewardVideo getInstance() {
        if (adManager == null) {
            adManager = new TrFBigoRewardVideo();
        }
        return adManager;
    }

    private RewardVideoAd mRewardVideoAd;

    public void loadRewardAd() {
        if (mRewardVideoAd == null) {
            mListner = null;
            loadAd();
        } else {
            if (mRewardVideoAd.isExpired()) {
                mRewardVideoAd.destroy();
                mRewardVideoAd = null;
                loadAd();
            }
        }
    }

    private void loadAd() {
        if (!loadFinish){
            return;
        }
        RewardVideoAdRequest rewardVideoAdAdRequest = new RewardVideoAdRequest.Builder()
                .withSlotId(TrFConstant.BIGO_REWARD_PLACEMENTID)
                .build();
        RewardVideoAdLoader rewardVideoAdAdLoader = new RewardVideoAdLoader.Builder().
                withAdLoadListener(new AdLoadListener<RewardVideoAd>() {

                    @Override
                    public void onError(@NonNull sg.bigo.ads.api.AdError adError) {
                        //load error
                        Log.d("TriviaW","bigo reward load error"+adError.getMessage());
                        loadFinish=true;
                        if (mListner != null) {
                            mListner.onAdError();
                        }
                    }

                    @Override
                    public void onAdLoaded(@NonNull RewardVideoAd ad) {
                        loadFinish=true;
                        mRewardVideoAd = ad;
                        mRewardVideoAd.setAdInteractionListener(new RewardAdInteractionListener() {

                            @Override
                            public void onAdRewarded() {
                                // It's time to offer some reward to the user.
                            }

                            @Override
                            public void onAdError(@NonNull sg.bigo.ads.api.AdError adError) {
                                // show error
                                if (mListner != null) {
                                    mListner.onAdError();
                                }
                            }

                            @Override
                            public void onAdImpression() {
                                // When the ad appears on the screen.
                                //Revenue
                            }

                            @Override
                            public void onAdClicked() {
                                // When the user clicks on the ad.
                            }

                            @Override
                            public void onAdOpened() {
                                // When the fullsceen ad covers the screen.
                            }

                            @Override
                            public void onAdClosed() {
                                // When the fullsceen ad closes.
                                loadAd();
                                if (mListner != null) {
                                    mListner.onAdOver();
                                }
                                mListner=null;
                            }
                        });
                        Log.d("TriviaW","bigo reward load success");
                    }
                }).build();
        loadFinish=false;
        rewardVideoAdAdLoader.loadAd(rewardVideoAdAdRequest);
    }

    public void showFullVideo(Activity activity, TrFVideoRewardCallback listner) {
        mListner=listner;
        if (mRewardVideoAd!=null && !mRewardVideoAd.isExpired()) {
            mRewardVideoAd.show();
        } else {
            if (mRewardVideoAd!=null) {
                mRewardVideoAd.destroy();
                mRewardVideoAd = null;
            }
            loadAd();
            if (mListner != null) {
                mListner.onAdError();
            }
        }
    }

    @Override
    public void showVideo(Activity activity, TrFVideoRewardCallback listner) {
        showFullVideo(activity, listner);
    }

    @Override
    public boolean isReady() {
        return mRewardVideoAd != null && !mRewardVideoAd.isExpired();
    }

    public void onDestroy(){
        if (mRewardVideoAd!=null){
            mRewardVideoAd.destroy();
            mRewardVideoAd=null;
        }
        mListner=null;
    }
}
